<?php
class Player {
    var $playerId;
    var $teamId;
    var $rosterNum;
    var $name;
    var $position;
    var $ma;
    var $st;
    var $ag;
    var $av;
    var $skills;
    var $injured = false;
    var $niggles = 0;
    var $completions = 0;
    var $tds = 0;
    var $interceptions = 0;
    var $casualties = 0;
    var $mvps = 0;
    var $exps = 0;
    var $cost;
    var $isPersisted = false;
    var $link;
    var $comments;

    function Player() {
        $this->playerId = -1 * (1 + rand(0, 1000000));
    }

    function init($row) {
        $i = 0;
        $this->teamId = $row[$i++];
        $this->playerId = $row[$i++];
        $this->rosterNum = $row[$i++];
        $this->name = $row[$i++];
        $this->position = $row[$i++];
        $this->ma = $row[$i++];
        $this->st = $row[$i++];
        $this->ag = $row[$i++];
        $this->av = $row[$i++];
        $this->skills = $row[$i++];
        $this->injured = $row[$i++] != 0;
        $this->niggles = $row[$i++];
        $this->completions = $row[$i++];
        $this->tds = $row[$i++];
        $this->interceptions = $row[$i++];
        $this->casualties = $row[$i++];
        $this->mvps = $row[$i++];
        $this->exps = $row[$i++];
        $this->cost = $row[$i++];
        if (isset($row[$i])) {
        	$this->comments = $row[$i++];
        }
        $this->isPersisted = true;
		$this->link="<a href=\"player_details.php?playerId=$this->playerId\" title=\"$this->name Details $this->comments\" class=\"playerName\">";
        return $this;
    }

    function getSPPs() {
        return $this->completions + (2 * $this->interceptions) + (2 * $this->casualties) + (3 * $this->tds) + (5 * $this->mvps) + ($this->exps);
    }

    function getInjuries() {
        $result = "&nbsp;";
        if ($this->injured) {
            $result = "mng";
        }

        if ($this->niggles > 0) {
            if ($this->injured) {
                $result = $result . " N";
            } else {
                $result = "N";
            }
            if ($this->niggles > 1) {
                $result = $result . "x" . $this->niggles;
            }
        }

        return $result;
    }

    function isJourneyman() {
        return !strncasecmp($this->position, "journey", 6);
    }

    function dead() {
        executeQuery("update players set dead=1 where player_id=$this->playerId");
        $this->isPersisted = false;
    }
    
    function save($team) {
        $this->teamId = $team->id;
        $name = addslashes($this->name);
        $position = addslashes($this->position);
        $skills = addslashes($this->skills);
        $comments = addslashes($this->comments);

        $rs = executeQuery("select completions, touchdowns, interceptions, casualties, mvps, exps from players where player_id=$this->playerId");
        if ($row = mysql_fetch_row($rs)) {
            if ((!isset ($this->cost)) or $this->cost == 0) {
                // delete existing entry
                $query = "update players set dead=1 where player_id=$this->playerId";
            } else {
                // update
                $query = "update players set name ='$name', position='$position', ma=$this->ma, st=$this->st, " .
                		"ag=$this->ag, av=$this->av, skills='$skills', injured=";
                if ($this->injured) {
                    $query = $query . "1";
                } else {
                    $query = $query . "0";
                }
                $query = $query . ", niggles=$this->niggles, completions=$this->completions, touchdowns=$this->tds, " .
                		"interceptions=$this->interceptions, casualties=$this->casualties, mvps=$this->mvps, " .
                		"exps=$this->exps, cost=$this->cost, comments='$comments', roster_num=$this->rosterNum " .
                		"where player_id=$this->playerId";
            }
        } else {
            // insert

            // don't add 0 cost players
            if (!(isset ($this->position) and isset ($this->cost) and strlen($this->position) > 0 and $this->cost > 0)) {
                return;
            }

            $query = "insert into players (fk_team_id, roster_num, name, position, ma, st, ag, av, skills, injured, niggles, completions, touchdowns, interceptions, casualties, mvps, exps, cost, comments) " .
            		"values ($team->id, $this->rosterNum, '$name', '$position', $this->ma, $this->st, $this->ag, " .
            		"$this->av, '$skills', ";
            if ($this->injured) {
                $query = $query . "1";
            } else {
                $query = $query . "0";
            }
            $query = $query . ", $this->niggles, $this->completions, $this->tds, $this->interceptions, " .
            		"$this->casualties, $this->mvps, $this->exps, $this->cost, '$comments')";
        }
        $rs = executeQuery($query);
        $this->isPersisted = $this->cost > 0;
    }

    function doStat(& $playerSkills, $difference, $firstPlus, $secondPlus, $firstMinus, $secondMinus) {
        global $skills;
        if ($difference == 0) {
            return;
        } else
            if ($difference == 1) {
                $playerSkills[$skills[$firstPlus]->id] = $skills[$firstPlus];
            } else
                if ($difference == -1) {
	                $playerSkills[$skills[$firstMinus]->id] = $skills[$firstMinus];
                } else
                    if ($difference == 2) {
		                $playerSkills[$skills[$firstPlus]->id] = $skills[$firstPlus];
		                $playerSkills[$skills[$secondPlus]->id] = $skills[$secondPlus];
                    } else
                        if ($difference == -2) {
			                $playerSkills[$skills[$firstMinus]->id] = $skills[$firstMinus];
			                $playerSkills[$skills[$secondMinus]->id] = $skills[$secondMinus];
                        }
    }

    function getSkills($race) {
        $playerSkills = array ();
        if (strlen($this->skills) > 0) {
            $splitSkills = split(",", $this->skills);
            foreach ($splitSkills as $plSk) {
                $plSk = trim($plSk);
                $skill = findSkill($plSk);
                if ($skill) {
                    // Ignore stats - get them later
                    if ($skill->id >= 75) {
                        continue;
                    }
                    $playerSkills[$skill->id] = $skill;
                }
            }
        }

        $position = $race->getPosition($this->position);
        $this->doStat($playerSkills, $this->ma - $position->ma, 75, 76, 83, 84);
        $this->doStat($playerSkills, $this->av - $position->av, 77, 78, 85, 86);
        $this->doStat($playerSkills, $this->ag - $position->ag, 79, 80, 87, 88);
        $this->doStat($playerSkills, $this->st - $position->st, 81, 82, 89, 90);

         return $playerSkills;
    }
}
?>